package ResourceManager;

import java.util.ArrayList;
import java.util.List;

import org.anddev.andengine.opengl.buffer.BufferObjectManager;
import org.anddev.andengine.opengl.texture.TextureOptions;
import org.anddev.andengine.opengl.texture.bitmap.BitmapTexture;
import org.anddev.andengine.opengl.texture.bitmap.BitmapTextureRegionFactory;
import org.anddev.andengine.opengl.texture.region.BaseTextureRegion;
import org.anddev.andengine.opengl.texture.region.TextureRegion;
import org.anddev.andengine.opengl.texture.region.TiledTextureRegion;
import org.anddev.andengine.ui.activity.BaseGameActivity;

import braindamagestudio.com.app.fight.SDCardBitmapTexureSource;

public class ResourceManager {
	
	TextureInformation[] TextureDictionary= 
	{
		new TextureInformation("Start.png",256,32),
		new TextureInformation("grass_background.png",1024,1024),
		new TextureInformation("sand.png",256,256),
		new TextureInformation("supriseSign.png",32,32),
		new TextureInformation("black.png",16,16),
		new TextureInformation("bigGreen.png",512,512),
		new TextureInformation("house.png",128,128),
		new TextureInformation("angleArrow.png",256,64),
		new TextureInformation("angleArrow2.png",256,64),
		new TextureInformation("downArrow.png",16,8),
		new TextureInformation("SDcardone.png",32,32),
		//map block
		new TextureInformation("grass.png",16,16),
		new TextureInformation("room_floor.png",16,16),
		new TextureInformation("room_floor2.png",16,16),
		new TextureInformation("rug.png",16,16),
		new TextureInformation("trees.png",16,16),
		new TextureInformation("wood_wall.png",16,16),
		new TextureInformation("wood_wall2.png",16,16),
		new TextureInformation("town_floor.png",16,16),
		new TextureInformation("room_floor3.png",16,16),
		
		//object
		new TextureInformation("tree.png",16,16),
		new TextureInformation("chair.png",16,16),
		new TextureInformation("table_left.png",16,16),
		new TextureInformation("table_mid.png",16,16),
		new TextureInformation("table_right.png",16,16),
		new TextureInformation("table_leftUp.png",16,16),
		new TextureInformation("table_rightUp.png",16,16),
		new TextureInformation("plant_home.png",16,16),
		new TextureInformation("window1.png",16,16),
		new TextureInformation("bed_verticle.png",16,32),
		new TextureInformation("stair_down.png",16,16),
		new TextureInformation("stair_up.png",16,16),
		new TextureInformation("computer.png",16,32),
		new TextureInformation("TV.png",16,16),
		new TextureInformation("bookshelf.png",16,32),
		new TextureInformation("blood.png",64,32),
		new TextureInformation("town_house1.png",64,64),
		new TextureInformation("house2.png",128,64),
		new TextureInformation("grass_mons.png",16,16),
		new TextureInformation("stone.png",16,16),
		new TextureInformation("waterpool.png",64,128),
		new TextureInformation("water.png",64,64),
		new TextureInformation("town_plant1.png",16,16),
		new TextureInformation("town_plant2.png",16,16),
		new TextureInformation("magic.png",64,64),
		new TextureInformation("connect_object.png",16,32),
		
		//NPC
		new TextureInformation("doctor2.png",32,32),
		new TextureInformation("girl1.png",32,32),
		new TextureInformation("girl2.png",32,32),
		new TextureInformation("mother1.png",32,32),
		new TextureInformation("mother2.png",32,32),
		
	};
	
	TiledTextureInformation[] TiledTextureDictionary= 
	{
		new TiledTextureInformation("Start.png",256,32,2,2),
		new TiledTextureInformation("character1.png",128,128,3,4),
		new TiledTextureInformation("character2.png",128,128,3,4),
		new TiledTextureInformation("doctor.png",128,128,3,4),
		new TiledTextureInformation("crawEffect.png",512,128,4,1),
		new TiledTextureInformation("basic.png",512,256,30,16),
		new TiledTextureInformation("boat.png",512,256,30,16),
		new TiledTextureInformation("inside.png",512,256,30,16),
		new TiledTextureInformation("outside.png",512,256,30,16),
		new TiledTextureInformation("puzzle.png",512,256,30,16),
		new TiledTextureInformation("SDCard.png",128,64,2,1),
	};
	
	BaseGameActivity _game;
	public List<BitmapTexture> _textures;
	public List<BaseTextureRegion> _regions;	
	public List<String> _resourseName;
	
	public ResourceManager(BaseGameActivity game)
	{
		_game = game;	
		_textures = new ArrayList<BitmapTexture>();
		_regions= new ArrayList<BaseTextureRegion>();
		_resourseName = new ArrayList<String>();
	}
	
	public TextureRegion IntelligentLoadTextureRegion(String resourceName)
	{
		if(isLoaded(resourceName))
		{
			return ((TextureRegion) getLoadedRegion(resourceName)).clone();
		}
		
		int width = 0;
		int height = 0;
		for(TextureInformation e : TextureDictionary)
		{
			if(e._resourceName.equals(resourceName))
			{
				width = e._width;
				height = e._height;
			}
		}
		
		BitmapTexture texture = new BitmapTexture(width, height, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		TextureRegion region = BitmapTextureRegionFactory.createFromAsset(texture, _game, resourceName, 0, 0);
		_game.getEngine().getTextureManager().loadTexture(texture);
		
		_resourseName.add(resourceName);
		_textures.add(texture);
		_regions.add(region);
		
				
		return region;		
	}
	
	public TextureRegion IntelligentLoadIconTextureRegion(String iconPath)
	{
		if(isLoaded(iconPath))
		{
			return ((TextureRegion) getLoadedRegion(iconPath)).clone();
		}
		
		BitmapTexture bitmapTexture = new BitmapTexture(32, 32, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		TextureRegion textureRegion = BitmapTextureRegionFactory.createFromSource(bitmapTexture, new SDCardBitmapTexureSource(_game,iconPath), 0, 0);
		_game.getEngine().getTextureManager().loadTexture(bitmapTexture);
		
		_resourseName.add(iconPath);
		_textures.add(bitmapTexture);
		_regions.add(textureRegion);
		
		return textureRegion;
	}
	
	public TiledTextureRegion IntelligentLoadTiledTextureRegion(String resourceName)
	{
		if(isLoaded(resourceName))
		{
			return ((TiledTextureRegion) getLoadedRegion(resourceName)).clone();
		}
		
		int width = 0;
		int height = 0;
		int columns = 0;
		int rows = 0;
		
		for(TiledTextureInformation e : TiledTextureDictionary)
		{
			if(e._resourceName.equals(resourceName))
			{
				width = e._width;
				height = e._height;
				columns = e._columns;
				rows = e._rows;
			}
		}
		
		BitmapTexture texture = new BitmapTexture(width, height, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		TiledTextureRegion region = BitmapTextureRegionFactory.createTiledFromAsset(texture, _game, resourceName, 0, 0, columns, rows);
		_game.getEngine().getTextureManager().loadTexture(texture);
		
		_resourseName.add(resourceName);
		_textures.add(texture);
		_regions.add(region);
		
		return region;
	}
	
	public void ReleaseAll()
	{
		for(BaseTextureRegion region : _regions)
		{	
		BufferObjectManager.getActiveInstance().unloadBufferObject(region.getTextureBuffer());
		}
		
		for(BitmapTexture texture : _textures)
		{	
		_game.getEngine().getTextureManager().unloadTexture(texture);
		}		
		
		_textures = new ArrayList<BitmapTexture>();
		_regions= new ArrayList<BaseTextureRegion>();
		_resourseName = new ArrayList<String>();
	}
	
	private boolean isLoaded(String resource)
	{
		if(_resourseName.contains(resource))
		{
			return true;
		}
		return false;
	}
	
	private BaseTextureRegion getLoadedRegion(String resource)
	{
		int index = _resourseName.indexOf(resource);
		return _regions.get(index);
	}
	
	public class TextureInformation
	{
		String _resourceName;
		int _width;
		int _height;
		
		public TextureInformation(String ResourceName,int Width,int Height)
		{
			_resourceName = ResourceName;
			_width = Width;
			_height = Height;
		}
	}
	
	public class TiledTextureInformation
	{
		String _resourceName;
		int _width;
		int _height;
		int _columns;
		int _rows;
		
		public TiledTextureInformation(String ResourceName,int Width,int Height,int Columns,int Rows)
		{
			_resourceName = ResourceName;
			_width = Width;
			_height = Height;
			_columns = Columns;
			_rows = Rows;
		}
	}
}
